//
//  PictureChooseView.swift
//  NewWeiBo
//
//  Created by 吴天益 on 2017/8/1.
//  Copyright © 2017年 ttt. All rights reserved.
//

import UIKit

class PictureChooseView: UICollectionView,UICollectionViewDataSource,UICollectionViewDelegate {
    
    
    //列数
    let col: CGFloat = 3
    //间隙
    let margin: CGFloat = 10

    //调用相机的闭包
    var clickAddPictureClusure: (()->())?
    
    lazy var picArray: [UIImage] = [UIImage]()
    
    //MARK: - 添加图片到数据源数组中
    func addImageToArray(image: UIImage){
        //最多9张图片
        if picArray.count >= 9 {
            return
        }
        
        picArray.append(image)
        
        reloadData()
    }

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        let flowLayout = UICollectionViewFlowLayout()
        
        flowLayout.minimumLineSpacing = margin
        flowLayout.minimumInteritemSpacing = margin
        
        super.init(frame: frame, collectionViewLayout: flowLayout)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //MARK: - 界面设置
    func setupUI(){
        
        self.dataSource = self
        self.delegate = self
        
        //注册
        register(pictureCell.self, forCellWithReuseIdentifier: "choose")
    }
    
    //MARK: - 数据源
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if picArray.count >= 9{
            return picArray.count
        }else{
            return picArray.count + 1
        }
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = dequeueReusableCell(withReuseIdentifier: "choose", for: indexPath) as! pictureCell
        
        if indexPath.item == picArray.count {
            
            cell.haveImage = false
            
            cell.imageView.image = UIImage(named: "compose_pic_add")
            
        }else{
            
            cell.haveImage =  true
            
            let image = picArray[indexPath.item]
            
            cell.imageView.image = image
        }
        
        cell.indexPath = indexPath
        
        //删除图片闭包赋值
        cell.deleteImageClusure = {[unowned self] (index)->() in
            
            self.picArray.remove(at: index)
            self.reloadData()
        }
        
        return cell
    }
    
    //MARK: - 代理方法,点击cell
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if indexPath.item == picArray.count {
            
            //添加图片
            clickAddPictureClusure?()
            
        }
    }
    
    
    //MARK: - 布局
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let itemWidth = (self.frame.size.width - 2 * margin) / col
 
        let flowLayout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
        flowLayout.itemSize = CGSize(width: itemWidth, height: itemWidth)
    }
  
}



//MARK:
//MARK: - 自定义图片collectionViewCell
class pictureCell: UICollectionViewCell {
    
    //判断是否有图
    var haveImage: Bool = true{
        didSet{
            //如果没有图片,是添加图片的就没有删除按钮
            if haveImage == false{
                deleteButton.isHidden = true
            }else{
                deleteButton.isHidden = false
            }

        }
    }
    
    //删除图片闭包
    var deleteImageClusure: ((Int)->())?
    
    //传递下标属性
    var indexPath: IndexPath?
    
    //懒加载imagV
    lazy var imageView = UIImageView()
    //删除按钮
    lazy var deleteButton: UIButton = {
        let button = UIButton()
        
        button.setImage(UIImage(named: "compose_photo_close"), for: .normal)
        button.addTarget(self, action: #selector(deleteImage(button:)), for: .touchUpInside)
    
        return button
    }()
    
   
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        addSubview(imageView)
        addSubview(deleteButton)
        
        imageView.snp.makeConstraints { (make) in
            make.edges.equalTo(self)
        }
        deleteButton.snp.makeConstraints { (make) in
            make.top.trailing.equalTo(imageView)
//            make.height.width.equalTo(10)
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 删除按钮的点击方法
    func deleteImage(button: UIButton) {
        //删除图片闭包调用
        deleteImageClusure?((indexPath?.item)!)
        
    }
    
    
}
